package org.example.code.test2025.three_month;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 *
 */
public class test0321<T> {
    private final T[] queue;
    private int front;
    private int size;
    private int rear;
    private final ReentrantLock lock = new ReentrantLock();
    private final Condition noFull = lock.newCondition();
    private final Condition noEmpty = lock.newCondition();


    public test0321(int size) {
        queue = (T[]) new Object[size];
        front=0;
        rear=0;
        this.size=0;
    }

    public void put(T t) throws InterruptedException {
        lock.lock();
        while(size==queue.length){
            noFull.await();
        }
        queue[rear]=t;
        rear=(rear+1)%queue.length;
        size++;
        noEmpty.signal();
    }

    public T take() throws InterruptedException {
        lock.lock();;
        while(size==0){
            noEmpty.await();;
        }
        T t = queue[front];
        front=(front+1)%queue.length;
        size--;
        noFull.signal();;
        return t;
    }
}
